using System;
using DG.Tweening;
using DL.Input;
using UnityEngine;
using UnityEngine.UI;

namespace DL.UI
{
    public class WaitPage : AbstractPage
    {
        
        #region ui component
        [SerializeField] private Image img_loading;
        private void Reset()
        {
            loadingRect = transform.Find("img_loading");
            img_loading = loadingRect.GetComponent<Image>();
        }
        #endregion

        private Tweener rotationTweener;
        private Transform loadingRect;
        
        public override void OnCreate()
        {
            loadingRect = transform.Find("img_loading");
            img_loading = loadingRect.GetComponent<Image>();
        }

        public override void OnRefresh()
        {
            var enable = Param.TryPopGet<bool>("enable");
            if (!enable)
            {
                Hide();
            }
        }

        public override void OnBringIn()
        {
            var enable = Param.TryPopGet<bool>("enable");
            if (enable)
            {
                Tween();
            }
        }

        private void Tween()
        {
            CharacterInputSystem.Inst.ReleaseControl();
            var rot = new Vector3(0, 0, 360);
            rotationTweener = loadingRect.DORotate(rot, 1.5f, RotateMode.FastBeyond360)
                .SetLoops(-1, LoopType.Restart); // 设置无限循环
        }

        public override void OnDismiss(bool destroyed)
        {
            if (rotationTweener != null && rotationTweener.IsPlaying())
            {
                rotationTweener.Kill();
            }
            CharacterInputSystem.Inst.GainControl();
        }
    }
}

